class Paiement:
    def __init__ (self, payer, receiver, amount, descrip):
        self._payer = payer
        self._receiver = receiver
        self._amount = amount
        self._description = descrip

    def __repr__ (self):
        return self._payer + " has paid " + str(self._amount) + " for " + self._receiver


class User:
    def __init__(self, name):
        self._name = name
        self._paiement_list = list()


class Counter:
    def __init__(self):
        self._user_list = list()
        self._paiement_list = list()

    def hasUser(self, name):
        has_user = False
        for user in self._user_list:
            if user._name == name:
                has_user = True

        return has_user
    
    def getUser(self, name):
        for user in self._user_list:
            if user._name == name:
                return user
        return None

    def initialisation(self, file_path):
        with open(file_path) as f:
            content = f.readlines()
            for line in content:
                items = line.split(' ', 3)
                pay = Paiement(items[0], items[1], float(items[2]), items[3])
                self._paiement_list.append(pay)
                
        for pay in self._paiement_list:
            if not self.hasUser(pay._payer):
                user = User(pay._payer)
                user._paiement_list.append(pay)
                self._user_list.append(user)
            else:
                self.getUser(pay._payer)._paiement_list.append(pay)
            
            if not self.hasUser(pay._receiver):
                user = User(pay._receiver)
                self._user_list.append(user)

    def showPaiement(self, userA, userB, pay_amount):
        if pay_amount != 0:
            print(userB,"should pay",pay_amount,"for",userA)
    
    def calPaiementUsers(self, userA, userB):
        result = 0
        payer = self.getUser(userA)
        receiver = self.getUser(userB)

        for pay in payer._paiement_list:
            if pay._receiver == userB:
                result += pay._amount

        for pay in receiver._paiement_list:
            if pay._receiver == userA:
                #print(userA, " ", userB)
                #print(pay)
                result -= pay._amount

        if result < 0:
            result = 0
                
        return result

    def calPaiement(self, user_name):
        for user in self._user_list:
            if user._name != user_name:
                self.showPaiement(user_name, user._name, self.calPaiementUsers(user_name, user._name))

    def calPaiementAll(self):
        for user in self._user_list:
            self.calPaiement(user._name)


##### Main #####
counter = Counter()
counter.initialisation("payments.txt")
counter.calPaiementAll()
